package com.clickpaas.materialsprocurement.dao.Impl;

import com.bizcloud.ipaas.t0ba0ba6afb694026b6b4e4e048af2b43.d20210202135416.codegen.SharewarehouseApi;
import com.bizcloud.ipaas.t0ba0ba6afb694026b6b4e4e048af2b43.d20210202135416.model.*;
import com.clickpaas.materialsprocurement.bean.MateralsDTOBean;
import com.clickpaas.materialsprocurement.constant.ConstantFile;
import com.clickpaas.materialsprocurement.dao.SharedInventDao;
import com.clickpaas.materialsprocurement.entity.SharedInventEntity;
import com.clickpaas.materialsprocurement.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Repository
public class SharedInventDaoImpl implements SharedInventDao {

    @Resource
    private MateralsDTOBean materalsDtoBean;
    @Resource
    private SharewarehouseApi sharewarehouseApi;

    /**
     * 根据条件查询共享库存数据
     *
     * @param sharedInventEntity
     */
    @Override
    public List<ShareWarehouseDTOResponse> getShareInventByCondtion(SharedInventEntity sharedInventEntity) {
        ShareWarehouseDTO shareWarehouseDTO = materalsDtoBean.shareWarehouseDTO();
        try {
            SpringUtil.copyPropertiesIgnoreNull(sharedInventEntity,shareWarehouseDTO);
        }catch (Exception e){
            log.info("对象转换失败！转换的对象是：【{}】",sharedInventEntity);
        }
        return queryShareInvent(shareWarehouseDTO);
    }

    /**
     * 共享库存对象数据入库
     *
     * @param sharedInventEntity
     */
    @Override
    public Boolean insertShareInvent(SharedInventEntity sharedInventEntity) {
        ShareWarehouseDTOUpdate shareWarehouseDTOUpdate = materalsDtoBean.shareWarehouseDTOUpdate();
        try {
            SpringUtil.copyPropertiesIgnoreNull(sharedInventEntity,shareWarehouseDTOUpdate);
        }catch (Exception e){
            log.info("对象转换失败！转换的对象是：【{}】",sharedInventEntity);
        }
        SaveOrUpdateShareWarehouseDTO saveOrUpdateShareWarehouseDTO = new SaveOrUpdateShareWarehouseDTO();
        saveOrUpdateShareWarehouseDTO.setQuery(materalsDtoBean.shareWarehouseDTOQuery());
        saveOrUpdateShareWarehouseDTO.setUpdate(shareWarehouseDTOUpdate);
        return insertData(saveOrUpdateShareWarehouseDTO);
    }

    private Boolean insertData(SaveOrUpdateShareWarehouseDTO saveOrUpdateShareWarehouseDTO){
        try {
            ShareWarehouseSaveOrUpdateDataResponseObject dataResponseString = sharewarehouseApi.saveOrUpdateShareWarehouseUsingPOST(saveOrUpdateShareWarehouseDTO);
            if (null != dataResponseString
                    && ConstantFile._MESSAGE.equals(dataResponseString.getMessage())
                    && null != dataResponseString.getData())
                return true;
        }catch (Exception e){
            log.info("采购订单入库失败：【{}】",e);
        }
        return false;
    }

    private List<ShareWarehouseDTOResponse> queryShareInvent(ShareWarehouseDTO shareWarehouseDTO){
        DataResponseListShareWarehouseDTO shareWarehouseUsingPOST = sharewarehouseApi.findShareWarehouseUsingPOST(shareWarehouseDTO);
        if(null == shareWarehouseUsingPOST
                || null == shareWarehouseUsingPOST.getData()
                || shareWarehouseUsingPOST.getData().size()<=0
                || !"SUCCESS".equals(shareWarehouseUsingPOST.getMessage())){
                log.info("当前查询的数据：【{}】",shareWarehouseUsingPOST);
            return null;
        }
        return shareWarehouseUsingPOST.getData();
    }
}
